package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.app.error.report.AppErrorReportDto
import com.opennews.openplatform.familyexpenseapi.service.AppErrorReportService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/app-error-report")
class AppErrorReportController(
    private val appErrorReportService: AppErrorReportService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @PostMapping("/send")
    fun send(@Valid @RequestBody data: AppErrorReportDto): ResponseEntity<ApiResponse<Any?>> {
        val claims = requestIdentityExtractor.getClaims()

        // If user is authenticated we need to get username.
        claims?.username?.let {
            data.username = it
        }

        appErrorReportService.save(data)
        return getSuccessResponse()
    }
}